import { Component, OnInit } from '@angular/core';
import {Category} from '../../../../../domain/Category';
import {CateApiService} from '../../../../services/cate/cate-api.service';
import {NzMessageService} from 'ng-zorro-antd';
import {Router} from '@angular/router';

@Component({
  selector: 'app-cate-list',
  templateUrl: './cate-list.component.html',
  styleUrls: ['./cate-list.component.less']
})
export class CateListComponent implements OnInit {

  dataSet: Category[]  = [];

  constructor(private cateApi: CateApiService, private msg: NzMessageService, private router: Router) { }

  ngOnInit() {
    this.getCategories();
  }

  private getCategories() {
    this.cateApi.categories().subscribe((response) => {
      if (response.success) {
          this.dataSet = response.data
      }
    })
  }

  public edit(cate: Category) {
    this.router.navigate(['/home/cate/update'],{queryParams:cate})
  }

  public delete(id) {
    console.log(id);
    this.cateApi.delete(id).subscribe((response) => {
      if (response.success) {
        this.msg.success('删除分类成功');
        this.getCategories();
      }
    })
  }
}
